package com.atguigu.eduservice.controller;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-02-24
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@Slf4j
//@CrossOrigin  //解决跨域
public class EduTeacherController {

    //访问地址： http://localhost:8001/eduservice/teacher/findAll
    //把service注入
    @Autowired
    private EduTeacherService teacherService;

    //1 查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        //调用service的方法实现查询所有的操作

        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("item",list);
    }
    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师id",required = true)
                                     @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        log.info("------->{}",b);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }

    }
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
//        LambdaQueryWrapper<EduTeacher> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(name!=null,EduTeacher::getName,name);
        teacherService.page(teacherPage,null);
        List<EduTeacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }
    @PostMapping("/pageTeachercondtion/{current}/{limit}")
    public R pageTeachercondtion(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(teacherQuery.getName()!=null,EduTeacher::getName,teacherQuery.getName());
        queryWrapper.eq(teacherQuery.getLevel()!=null,EduTeacher::getLevel,teacherQuery.getLevel());
        queryWrapper.ge(teacherQuery.getBegin()!=null,EduTeacher::getGmtCreate,teacherQuery.getBegin());
        queryWrapper.le(teacherQuery.getEnd()!=null,EduTeacher::getGmtCreate,teacherQuery.getEnd());
        queryWrapper.orderByDesc(EduTeacher::getGmtCreate);

        teacherService.page(teacherPage,queryWrapper);
        List<EduTeacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return R.ok().data("total",total).data("rows",records);

    }

    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.save(eduTeacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
//    /eduservice/teacher/getTeacher/${id}
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if (b){
            return R.ok();
        }
        return R.error();
    }
}

